/**
 * Ingenius-Group S.A.S
 *
 * Project: SAU
 * File: EditFactorPresenter.java
 * Date: 07/02/2011
 */

package com.ingenius.sau.client.presenter;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.ingenius.sau.client.AppController;
import com.ingenius.sau.client.Constants;
import com.ingenius.sau.client.event.ListFactorsEvent;
import com.ingenius.sau.client.service.GuidelinesServiceAsync;
import com.ingenius.sau.client.view.Display;
import com.ingenius.sau.shared.model.guidelines.Factor;
import com.ingenius.sau.shared.model.guidelines.Guideline;

/**
 * Class <strong>EditFactorPresenter</strong>
 * 
 * @author Jesus David
 * @version 0.1
 */
public class EditFactorPresenter implements EditPresenter {

	public interface EditFactorDisplay extends Display {

		String getName();

		void setName(String name);

		String getDescription();

		void setDescription(String description);

		void show();

		void hide();
	}

	private final HandlerManager eventBus;
	private final GuidelinesServiceAsync rpcService;
	private final EditFactorDisplay display;
	private Factor factor;

	public EditFactorPresenter(HandlerManager eventBus,
			GuidelinesServiceAsync rpcService, EditFactorDisplay display,
			String factorId) {

		this.rpcService = rpcService;
		this.eventBus = eventBus;
		this.display = display;
		display.setPresenter(this);

		if (factorId == null) {
			factor = new Factor();

		} else {
			loadBean(factorId);
		}

	}

	private void loadBean(String factorId) {
		AppController.setStatusMessage("Loading...");
		rpcService.getFactor(factorId, new AsyncCallback<Factor>() {
			@Override
			public void onSuccess(Factor result) {
				if (result == null) {
					onFailure(new Throwable("Database error"));
					return;
				}
				factor = result;
				display.setName(factor.getName());
				display.setDescription(factor.getDescription());
				hideStatusMessage();
			}

			@Override
			public void onFailure(Throwable caught) {
				GWT.log(Constants.MESSAGE_FAILED_GET + Factor.class.getName(),
						caught);
				factor = new Factor();
				// TODO i18n
				String failedMessage = "Failed to retrieve factor";
				display.showInfo(failedMessage);
				hideStatusMessage();
			}

			void hideStatusMessage() {
				AppController.hideStatusMessage();
			}
		});
	}

	@Override
	public void doSaveOrUpdate() {
		factor.setName(display.getName());
		factor.setDescription(display.getDescription());
		String currentGuidelineId = (String) AppController
				.getAttribute(Constants.CURRENT_GUIDELINE_ID);
		factor.setGuideline(new Guideline(currentGuidelineId));

		// TODO i18n
		final String failedMessage = "Failed to save factor";
		final String successMessage = "Factor saved sucessfully";

		display.hide();
		AppController.setStatusMessage("Saving...");
		rpcService.saveOrUpdateFactor(factor, new AsyncCallback<Boolean>() {
			@Override
			public void onSuccess(Boolean result) {
				if (result) {
					eventBus.fireEvent(new ListFactorsEvent());
					display.showInfo(successMessage);
					hideStatusMessage();

				} else {
					onFailure(new Throwable("Database error"));
				}
			}

			@Override
			public void onFailure(Throwable caught) {
				GWT.log(Constants.MESSAGE_FAILED_SAVE + Factor.class.getName(),
						caught);
				display.showInfo(failedMessage);
				hideStatusMessage();
			}

			void hideStatusMessage() {
				AppController.hideStatusMessage();
			}
		});
	}

	@Override
	public void go(HasWidgets container) {
		display.show();
	}

}
